var EpgGrid = Class.create(EpgWidget,{
	initialize: function($super, id, opts,links) {
        $super(id,opts,links);
        this.begin = 0;
        this.current = null;
        this.size  = this.opts.row * this.opts.col;
        this.skip = 0;
        if(opts.skip){
        	this.skip = opts.skip;
        }
        if(opts.pageable){
        	this.enableUp = opts.enableUp;
        }else{
        	this.enableUp = true;
        }
        if(opts.pageable){
        	this.enableDown = opts.enableDown;
        }else{
        	this.enableDown = true;
        }
    },
    refershScollerBar : function() {
		var bar = $("#" + this.id + "Scroller_img");
		var scroller = $("#" + this.id + "Scroller_bar");
		var cssHeight = scroller.css("height");
		cssHeight = cssHeight.replace("px", "");
		var height = parseInt(cssHeight);
		var barHeight = 0;
		if (this.opts.pageCount > 0) {
			barHeight = Math.floor(height / this.opts.pageInfo.pageCount);
			bar.css("height", barHeight + "px");
		}
		barTop = barHeight * (this.opts.pageInfo.pageNo - 1);
		if (barTop + barHeight > height) {
			barTop = height - barHeight;
		}
		bar.css("top", barTop + "px");
	},

	isCell : function(aId) {
		console.log("aId:"+aId);
		console.log("this.flag:"+this.flag);
		return aId.indexOf(this.flag) == 0
	},
	setLinks:function(count){
		if(this.links.length>=count){
			 for(var i=count;i<this.links.length;i++){
					var obj =  epgPage.findLink(this.links[i]);
					obj.disable = true;
					$("#"+this.opts.layoutId+i).hide();
			 }
		}else{
			 for(var i=0 ;i<this.links.length;i++){
					var obj =  epgPage.findLink(this.links[i]);
					obj.disable = false;
			 }
		}
	},
	
	pageUp : function(idx) {
			if(this.skip>0 && this.opts.pageInfo.pageNo==2){
				EPG.gotoSelf(this.opts.pageInfo.prevUri,this.links[idx]);
				return true;
			}
			if (this.opts.pageInfo.pageNo > 1) {
				idx = this.size-this.opts.col +idx ;
				if(idx<this.links.length){
					EPG.gotoSelf(this.opts.pageInfo.prevUri,this.links[idx]);
				}else{
					var aIdx = this.links.length-1;
					var lastId = this.links[aIdx];
					var newId = lastId.replace("_"+aIdx,"_"+idx);
					EPG.gotoSelf(this.opts.pageInfo.prevUri,newId);
				}
				
				return true;
			} else {
				return false;
			}
	
	},
	pageDown : function(idx) {
		if (this.opts.pageInfo.pageNo < this.opts.pageInfo.pageCount) {
			var idx_start = this.opts.col- (this.size-idx);
			idx = this.opts.col- (this.size-idx);
			if(0>idx) { idx = 0; }
		    if(this.opts.pageInfo.pageNo==this.opts.pageInfo.pageCount-1){
			  var lastidx = (this.opts.pageInfo.recordCount % this.size)%this.opts.col;  
			  if(idx>=lastidx){
				  idx = lastidx-1;
			  }
			  if(idx == -1){
				  idx = idx_start%this.opts.col;
			  }
		   }
		   EPG.gotoSelf(this.opts.pageInfo.nextUri,this.links[idx]);
			return true;
		} else {
			return false;
		}
	},
 	focus:function(id){
 		if(epgPage.currentWidget != this){
 			idx = this.index(id);
 			if(idx<0){
 				id  = this.links[this.links.length-1];
 			}
 			if(id){
 				epgPage.focus(id);
 				this.current = id;
 				return true;
 			}else{	
 				if(this.links.length>0){
 					epgPage.foucs(this.links[0]);
 					this.current = this.links[0];
 					return true;
 				}
 			}
 		}
		
		return false;
	},
	blur:function(){
		if(this.current){
			epgPage.blur(this.current);
			this.current = null;
		}
	},
	onkeypress : function(ev) {
		var aId = ev.target.id;
		keycode = ev.which;
		var idx = this.index(aId);
		var link  = epgPage.findLink(aId);
		var cell = new Cell(this.opts.col,idx,this.links.length-1);
		
		switch (keycode) {
			case KEY_ENUM.KEY_UP:
				if(cell.colIdx==0 && cell.rowIdx>0){
					epgPage.switchFocus(this.links[idx-this.opts.col]);
					return true;
				}
				next = epgPage.findNextLink(link.direction.upId,KEY_ENUM.KEY_UP);
				if (cell.isFirstRow() && this.enableUp) {
					return this.pageUp(idx);
				} else {
					return false;
				}
			case KEY_ENUM.KEY_DOWN:
				if(idx==0){
					if(this.links.length > this.opts.col){
						epgPage.switchFocus(this.links[this.opts.col]);
						return true;
					}
				}
			    next = epgPage.findNextLink(link.direction.downId,KEY_ENUM.KEY_DOWN);
				if (cell.isLastRow() && this.enableDown) {
					return this.pageDown(idx);
				} else {
					return false;
				}
			case KEY_ENUM.KEY_LEFT:{
				var next = epgPage.findNextLink(link.direction.leftId,KEY_ENUM.KEY_LEFT);
				if(cell.isLeftCell() && next==null ){
					epgPage.switchFocus(this.links[idx-1]);
					return true;
				}
				return false;
			}
			case KEY_ENUM.KEY_RIGHT:{
				var next = epgPage.findNextLink(link.direction.rightId,KEY_ENUM.KEY_RIGHT);

				if(cell.isRightCell() && next==null ){
					epgPage.switchFocus(this.links[idx+1]);
					return true;
				}
				return false;
			}
			case KEY_ENUM.KEY_PAGEUP:{
				return this.pageUp(0);
			}
			case KEY_ENUM.KEY_PAGEDOWN:{
				return this.pageDown(0);
			}
		}
		return false;
	}
	
});




var Cell = function(col,idx,lastIdx){
		this.rowIdx = Math.floor(idx/ col);
		this.colIdx = Math.ceil(idx % col);
		this.rowCount = Math.floor(lastIdx/col);
	
		this.isFirstRow = function() {
			return this.rowIdx == 0;
		};
		this.isLastRow = function() {
			return this.rowIdx == this.rowCount;
		};
		this.isLeftCol = function() {
			return this.colIdx == 0;
		};
		this.isRightCol = function() {
			return this.colIdx == col-1;
		}
		this.isLeftCell = function(){
			return this.isLeftCol() && !this.isFirstRow()
		}
		this.isRightCell = function(){
			return this.isRightCol() && !this.isLastRow()
		}
		this.isLastCell =function(){
			return lastIdx==idx;
		}
}
